package com.jjwxc.jwjskandriod.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;

import com.alibaba.fastjson.JSON;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.ScreenResponse;

import java.util.ArrayList;
import java.util.List;

public class ScreenPop {

    String bookType;
    String readingRate;
    String sortType;
    String subscribeStatus;

    public void screenPopWindow(View v, Activity context, String screen, ScreenListener screenListener) {
        View view = LayoutInflater.from(context).inflate(R.layout.screen_pop, null, false);

        TagGroupView tgv_one = (TagGroupView) view.findViewById(R.id.tgv_one);
        TagGroupView tgv_two = (TagGroupView) view.findViewById(R.id.tgv_two);
        TagGroupView tgv_three = (TagGroupView) view.findViewById(R.id.tgv_three);
        TagGroupView tgv_four = (TagGroupView) view.findViewById(R.id.tgv_four);
        LinearLayout ll_quxiao = (LinearLayout) view.findViewById(R.id.ll_quxiao);

        //1.构造一个PopupWindow，参数依次是加载的 View，宽高
        final PopupWindow popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        //设置加载动画
        popWindow.setAnimationStyle(R.style.PopupWindow);
        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题

        popWindow.setTouchable(true);
        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setBackgroundAlpha(1, context);
                ScreenResponse screenResponse = new ScreenResponse();
                screenResponse.setBookType(bookType);
                screenResponse.setReadingRate(readingRate);
                screenResponse.setSortType(sortType);
                screenResponse.setSubscribeStatus(subscribeStatus);
                screenListener.onValueClick(JSON.toJSONString(screenResponse));
            }
        });

        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效
        //设置 popupWindow 显示的位置，参数依次是参照 View，x轴的偏移量，y轴的偏移量
        popWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);

        setBackgroundAlpha(0.3f, context);
        ll_quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ScreenResponse screenResponse = JSON.parseObject(screen, ScreenResponse.class);
        if (screenResponse != null) {
            bookType = screenResponse.getBookType();
            readingRate = screenResponse.getReadingRate();
            sortType = screenResponse.getSortType();
            subscribeStatus = screenResponse.getSubscribeStatus();
        }
        //设置 popupWindow 里的按钮的事件
        List<String> list = new ArrayList<>();
        list.add("已读");
        list.add("未读");
        list.add("读完");
        tgv_one.requestShowMeasure(true);
        tgv_one.setRadioButtonTagsByScren(list, R.layout.screen_pop_toggle);
        tgv_one.checkSelecteDefault(StUtils.isEmpty(screenResponse.getReadingRate()) ? -1 : Integer.parseInt(screenResponse.getReadingRate()) - 1);
        tgv_one.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId > -1) {
                    readingRate = tgv_one.getRadioButtonCheck() + "";
                } else {
                    readingRate = "";
                }
            }
        });

        List<String> list2 = new ArrayList<>();
        list2.add("已购买");
        list2.add("未购买");
        tgv_two.requestShowMeasure(true);
        tgv_two.setRadioButtonTagsByScren(list2, R.layout.screen_pop_toggle);
        tgv_two.checkSelecteDefault(StUtils.isEmpty(screenResponse.getSubscribeStatus()) ? -1 : Integer.parseInt(screenResponse.getSubscribeStatus()) - 1);
        tgv_two.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId > -1) {
                    subscribeStatus = tgv_two.getRadioButtonCheck() + "";
                } else {
                    subscribeStatus = "";
                }
            }
        });
        List<String> list3 = new ArrayList<>();
        list3.add("付费书");
        list3.add("包月书");
        list3.add("免费书");
        tgv_three.requestShowMeasure(true);
        tgv_three.setRadioButtonTagsByScren(list3, R.layout.screen_pop_toggle);
        tgv_three.checkSelecteDefault(StUtils.isEmpty(screenResponse.getBookType()) ? -1 : Integer.parseInt(screenResponse.getBookType()) - 1);
        tgv_three.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId > -1) {
                    bookType = tgv_three.getRadioButtonCheck() + "";
                } else {
                    bookType = "";
                }
            }
        });
        List<String> list4 = new ArrayList<>();
        list4.add("收藏时间");
        list4.add("最近阅读");
        tgv_four.requestShowMeasure(true);
        tgv_four.setRadioButtonTagsByScren(list4, R.layout.screen_pop_toggle);
        tgv_four.checkSelecteDefault(StUtils.isEmpty(screenResponse.getSortType()) ? -1 : Integer.parseInt(screenResponse.getSortType()) - 1);
        tgv_four.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId > -1) {
                    sortType = tgv_four.getRadioButtonCheck() + "";
                } else {
                    sortType = "";
                }
            }
        });
    }

    public interface ScreenListener {
        void onValueClick(String value);
    }

    public void setBackgroundAlpha(float alpha, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        activity.getWindow().setAttributes(lp);
    }
}
